package com.infore.airsupervise.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.infore.airsupervise.ui.activity.BaseActivity;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;

/**
 * 界面跳转工具类
 *
 * @author: dingxinlin
 */
public class JumpUtil {

    /**
     * 无传值的跳转
     */
    public static void jumpFromTo(Context context, Class<?> _class) {
        Intent intent = new Intent();
        intent.setClass(context, _class);
        context.startActivity(intent);
    }

    /**
     * 传递int的跳转
     */
    public static void jumpFromTo(
            Context context, Class<?> _class, String key, int Value) {
        Intent intent = new Intent();
        intent.putExtra(key, Value);
        intent.setClass(context, _class);
        context.startActivity(intent);
    }

    /**
     * 传递string的跳转
     */
    public static void jumpFromTo(
            Context context, Class<?> _class, String key, String Value) {
        Intent intent = new Intent();
        intent.putExtra(key, Value);
        intent.setClass(context, _class);
        context.startActivity(intent);
    }

    /**
     * 传递object的跳转
     */
    public static void jumpFromTo(
            Context context, Class<?> _class, String key, Object object) {
        Intent intent = new Intent(context, _class);
        intent.putExtra(key, (Serializable) object);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 传递字符串map的跳转
     */
    public static void jumpFromTo(
            Context context, Class<?> _class, Map<String, String> map) {
        Intent intent = new Intent(context, _class);
        // 遍历map
        Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            intent.putExtra(entry.getKey(), entry.getValue());
        }
        context.startActivity(intent);
    }

    public static void jumpFromTo(
            Context context, Class<?> _class, Bundle bundle) {
        Intent intent = new Intent(context, _class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /**
     * 带返回值的传递bundle的跳转
     */
    public static void ForResultJumpFromTo(
            BaseActivity from, Class<?> _class, Bundle bundle,int requestCode) {
        Intent intent = new Intent(from, _class);
        intent.putExtras(bundle);
        from.startActivityForResult(intent,requestCode);
    }


    public static void ForResultJumpFromTo(BaseActivity from, Class<?> _class, int requestCode){
        Intent intent = new Intent(from, _class);
        from.startActivityForResult(intent,requestCode);
    }


    public static void ForResultJumpFromTo(BaseActivity from, Class<?> _class, int requestCode,String key,int value){
        Intent intent = new Intent(from, _class);
        intent.putExtra(key,value);
        from.startActivityForResult(intent,requestCode);
    }

    public static void ForResultJumpFromTo(BaseActivity from, Class<?> _class, int requestCode, String key, Object object){
        Intent intent = new Intent(from, _class);
        intent.putExtra(key, (Serializable) object);
        from.startActivityForResult(intent,requestCode);
    }

    public static void ForResultJumpFromTo(BaseActivity from, Class<?> _class, int requestCode, Bundle bundle){
        Intent intent = new Intent(from, _class);
        intent.putExtras(bundle);
        from.startActivityForResult(intent,requestCode);
    }
}
